﻿using System;
using System.Globalization;
using System.IO;
using System.util;
using System.Xml.Linq;
using iTextSharp.text;

namespace PocoReport
{
    public class Image : IPart, IResizablePart
    {
        public Style Style { get; protected set; }
        public string FileName { get; set; }
        public string DataSource { get; set; }
        
        public Image()
        {
            Style = new Style();
        }

        public void Load(XNode node)
        {
            XElement element = (XElement)node;
            Style = new Style(element);
            FileName = element.GetAttribValue("FileName", null);
            DataSource = element.GetAttribValue("DataSource", null);
        }

        public XNode Save()
        {
            XElement element = new XElement("Image");
            element.SetAttribValue("Style", Style);
            element.SetAttribValue("FileName", FileName);
            element.SetAttribValue("DataSource", DataSource);
            return element;
        }

        public IElement GetPdfElement(Document document, Style parentStyle, Scope scope)
        {
            iTextSharp.text.Image image;
            if (string.IsNullOrEmpty(DataSource))
            {
                image = iTextSharp.text.Image.GetInstance(FileName);
            }
            else
            {
                object value = scope.GetValue(DataSource);
                Stream stream = value as Stream;
                if (stream != null)
                {
                    image = iTextSharp.text.Image.GetInstance(stream);
                }
                else
                {
                    byte[] bytes = value as byte[];
                    if (bytes != null)
                    {
                        image = iTextSharp.text.Image.GetInstance(bytes);
                    }
                    else
                    {
                        throw new ApplicationException("Invalid Image Type");
                    }
                }
            }
            Style mergedStyle = Style.Merge(parentStyle, Style);
            SetProperties(image, mergedStyle.Properties);
            return new Chunk(image, 0, 0);
        }

        internal static void SetProperties(iTextSharp.text.Image image, Properties attributes)
        {
            string attribute = attributes["align"];
            int num = 0;
            if (attribute != null)
            {
                if (Util.EqualsIgnoreCase("Left", attribute))
                {
                    num = num;
                }
                else if (Util.EqualsIgnoreCase("Right", attribute))
                {
                    num |= 2;
                }
                else if (Util.EqualsIgnoreCase("Middle", attribute))
                {
                    num |= 5;
                }
            }
            if (Util.EqualsIgnoreCase("true", attributes["underlying"]))
            {
                num |= 8;
            }
            if (Util.EqualsIgnoreCase("true", attributes["textwrap"]))
            {
                num |= 4;
            }
            image.Alignment = num;
            attribute = attributes["alt"];
            if (attribute != null)
            {
                image.Alt = attribute;
            }
            string s = attributes["absolutex"];
            string str3 = attributes["absolutey"];
            if ((s != null) && (str3 != null))
            {
                image.SetAbsolutePosition(float.Parse(s, NumberFormatInfo.InvariantInfo), float.Parse(str3, NumberFormatInfo.InvariantInfo));
            }
            attribute = attributes["plainwidth"];
            if (attribute != null)
            {
                image.ScaleAbsoluteWidth(float.Parse(attribute, NumberFormatInfo.InvariantInfo));
            }
            attribute = attributes["plainheight"];
            if (attribute != null)
            {
                image.ScaleAbsoluteHeight(float.Parse(attribute, NumberFormatInfo.InvariantInfo));
            }
            attribute = attributes["rotation"];
            if (attribute != null)
            {
                image.Rotation = float.Parse(attribute, NumberFormatInfo.InvariantInfo);
            }
        }

 

    }

    public interface IResizablePart
    {
        
    }
}
